package com.sean.gulimall.product.controller;

import com.sean.common.utils.PageUtils;
import com.sean.common.utils.R;
import com.sean.gulimall.product.service.AttrService;
import com.sean.gulimall.product.vo.AttrResponseVo;
import com.sean.gulimall.product.vo.AttrVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 商品属性
 *
 * @author sean
 * @email sean_xin@126.com
 * @date 2022-05-22 21:41:07
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    /**
     * 基本属性列表
     * catelogId:产品分类Id
     */
    @RequestMapping("/base/list/{catelogId}")
    @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long categoryId){
        PageUtils page = attrService.queryAttrPageByCatelogId(params,categoryId,1);
        return R.ok().put("page", page);
    }

    /**
     * 销售属性列表
     * catelogId:产品分类Id
     */
    @RequestMapping("/sale/list/{catelogId}")
    @RequiresPermissions("product:attr:list")
    public R saleList(@RequestParam Map<String, Object> params,@PathVariable("catelogId")Long categoryId){
        PageUtils page = attrService.queryAttrPageByCatelogId(params,categoryId,0);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		AttrResponseVo attr = attrService.getAttrAllInfo(attrId);
        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
        return  attrService.removeAttrByIds(Arrays.asList(attrIds));
    }

}
